<?php

class VanillaPostPeer extends BaseVanillaPostPeer
{
    public static function getLatestPost() {
        $c = new Criteria();
        $c->addDescendingOrderByColumn(self::DATE);
        $result = self::doSelectWithI18n($c);

        return $result[0];
    }

    public static function getLastPost() {
        $c = new Criteria();
        $c->addDescendingOrderByColumn(self::DATE);
        $c->setLimit(sfConfig::get('app_max_post'));
        return self::doSelectWithI18n($c);
    }

    public static function getYears() {
        $c = new Criteria();
        $c->addAscendingOrderByColumn(self::DATE);

        $results = self::doSelect($c);

        $date = array();
        
        foreach($results as $result) {
            $stringaAnno = substr($result->getDate() , 0, 4);
            $stringaMese = substr($result->getDate(), 5, 2);

            $date[] = array('anno' => $stringaAnno, 'mese' => $stringaMese);
        }

        return $date;
    }

    public static function getByMonth($month, $year) {
        $c = new Criteria();
        $c->add(self::DATE, "YEAR(".self::DATE.") = '".$year."'", Criteria::CUSTOM);
        $c->addAnd(self::DATE, "MONTH(".self::DATE.") = '".$month."'", Criteria::CUSTOM);
        $c->addDescendingOrderByColumn(self::DATE);
        $c->setLimit(sfConfig::get('app_max_post'));

        return self::doSelect($c);
    }
}
